package com.sky.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private WebSocketServer webSocketServer;



    @PostMapping("submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单：{}",ordersSubmitDTO);

        OrderSubmitVO orderSubmitVO=orderService.submit(ordersSubmitDTO);

        return Result.success(orderSubmitVO);
    }


    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")

    public Result<PageResult> page(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("历史订单查询:{}", BaseContext.getCurrentId());
        PageResult result=orderService.page(ordersPageQueryDTO);

        return Result.success(result);

    }

    @GetMapping("orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> getByOrderId(@PathVariable Long id){
        log.info("查询订单详情:{}",id);

        OrderVO orderVO= orderService.getById(id);

        //log.info(String.valueOf(orderVO.getTablewareNumber()));
        return Result.success(orderVO);
    }



    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }


    @PutMapping("cancel/{id}")
    @ApiOperation("取消订单")
    public Result<String> cancel( @PathVariable Long id, @RequestBody OrdersCancelDTO ordersCancelDTO){
        log.info("取消订单 订单id：{},原因：{}",id,ordersCancelDTO);

        ordersCancelDTO.setId(id);
        orderService.cancel(ordersCancelDTO);

        return Result.success();
    }


    @GetMapping("/reminder/{id}")
    @ApiOperation("催单")
    public Result<String> remind(@PathVariable Long id){
        log.info("催单 订单id：{}",id);
        orderService.remind(id);


        return Result.success();
    }

}
